package com.example.myys.ui.page.settingPage

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.myys.Constants
import com.example.myys.Constants.Companion.darkModeState
import com.example.myys.viewModel.settingModel.SettingViewModel

//设置展示
@Composable
fun Setting(settingViewModel: SettingViewModel = viewModel(), modifier: Modifier = Modifier) {
    val ysState by settingViewModel.ysState.collectAsState()
    val xsState by settingViewModel.xsState.collectAsState()
    val mhState by settingViewModel.mhState.collectAsState()
    val systemState by settingViewModel.systemState.collectAsState()

    // 创建一个可变的状态来跟踪选中的选项
    var selectedOption by remember { mutableStateOf("") }
    var selectedOptionXs by remember { mutableStateOf("") }
    var selectedOptionMh by remember { mutableStateOf("") }


    Column(
        modifier = Modifier
            .padding(3.dp, 2.dp)
            .fillMaxSize()
            .verticalScroll(rememberScrollState()),
    ) {
        Column(
            modifier = Modifier.heightIn(max = 300.dp)
        ) {
            Text(
                text = "系统配置",
                style = MaterialTheme.typography.titleLarge
            )
            LazyColumn {
                itemsIndexed(systemState) { index, item ->
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(text = item.name)
                        Switch(checked = item.isChecked == 1, onCheckedChange = {
                            var itemi = item
                            if (it) {
                                itemi = item.copy(isChecked = 1)
                            } else {
                                itemi = item.copy(isChecked = 0)
                            }
                            settingViewModel.saveSetting(itemi)
                            //全局变量
                            darkModeState.value = it
                        }
                        )
                    }
                }
            }

        }
        HorizontalDivider()
        Column(
            modifier = Modifier.heightIn(max = 300.dp)
        ) {
            Text(
                text = "影视",
                style = MaterialTheme.typography.titleLarge
            )
            LazyColumn {
                itemsIndexed(ysState) { index, item ->
                    if (item.isChecked == 1) {
                        selectedOption = item.name
                    }
                    Row {
                        Row(
                            modifier = Modifier
                                .weight(1f)
                                .clickable {
                                    selectedOption = item.name
                                    settingViewModel.updateIsChecked(Constants.YS, item)
                                }
                                .padding(2.dp, 2.dp)
                                .background(
                                    MaterialTheme.colorScheme.primaryContainer,
                                    MaterialTheme.shapes.medium
                                ),
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Text(
                                modifier = Modifier.padding(5.dp, 0.dp),
                                text = (index + 1).toString()
                            )
                            Text(
                                modifier = Modifier.weight(1f),
                                text = item.name
                            )
                            RadioButton(
                                selected = item.name == selectedOption,
                                onClick = { })
                        }
                        Button(onClick = { settingViewModel.deleteSeting(Constants.YS, item) }) {
                            Text(text = "删除")
                        }
                    }
                }
            }
        }
        HorizontalDivider()
        Column(
            modifier = Modifier.heightIn(max = 300.dp)
        ) {
            Text(
                text = "小说",
                style = MaterialTheme.typography.titleLarge
            )
            LazyColumn {
                itemsIndexed(xsState) { index, item ->
                    if (item.isChecked == 1) {
                        selectedOptionXs = item.name
                    }
                    Row(
                        modifier = Modifier
                            .clickable {
                                selectedOptionXs = item.name
                                settingViewModel.updateIsChecked(Constants.XS, item)
                            }
                            .padding(2.dp, 2.dp)
                            .background(
                                MaterialTheme.colorScheme.primaryContainer,
                                MaterialTheme.shapes.medium
                            ),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            modifier = Modifier.padding(5.dp, 0.dp),
                            text = (index + 1).toString()
                        )
                        Text(
                            modifier = Modifier.weight(1f),
                            text = item.name
                        )
                        RadioButton(
                            selected = item.name == selectedOptionXs,
                            onClick = { })
                    }
                }
            }
        }
        HorizontalDivider()
        Column(
            modifier = Modifier.heightIn(max = 300.dp)
        ) {
            Text(
                text = "漫画",
                style = MaterialTheme.typography.titleLarge
            )
            LazyColumn {
                itemsIndexed(mhState) { index, item ->
                    if (item.isChecked == 1) {
                        selectedOptionMh = item.name
                    }
                    Row(
                        modifier = Modifier
                            .clickable {
                                selectedOptionMh = item.name
                                settingViewModel.updateIsChecked(Constants.MH, item)
                            }
                            .padding(2.dp, 2.dp)
                            .background(
                                MaterialTheme.colorScheme.primaryContainer,
                                MaterialTheme.shapes.medium
                            ),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            modifier = Modifier.padding(5.dp, 0.dp),
                            text = (index + 1).toString()
                        )
                        Text(
                            modifier = Modifier.weight(1f),
                            text = item.name
                        )
                        RadioButton(
                            selected = item.name == selectedOptionMh,
                            onClick = { })
                    }
                }
            }
        }


    }
}

@Preview
@Composable
fun setingView() {
    Setting()
}
